<?php
//dragon.hu add @2015.12.23  businessrecord

defined ( 'BASEPATH' ) or die ( 'forbidden' );
class Businessrecord_Controller extends Controller {
	private $type;
	private $name;
	private $begintime;
	private $endtime;
	private $tmpcontent;
	private $content;
	
	function _init() {
		$this->type = array (
				'1' => translate ( 'TYPE1' ),
				'2' => translate ( 'TYPE2' ) 
		);
		check_level(50, segment(0), segment(1));
		set_time_limit(0);
		
		$rows = array();
		$rowdata = array();

		$this->db->query("SELECT * FROM {pre}area_current");
		while ($row = $this->db->get_array()) {
			$rows[$row['id']] = $row;
		}
		
		$sql = "SELECT * FROM tt_businessserver";
		$this->db->query($sql);
		while ($row = $this->db->get_array())
		{
			$busrows[$row['area']][$row['downtime']] = $row;
		}
		
		foreach ($rows as $key => $value)
		{
			$tmparrinfo = get_area_list_option();
			$rowdata['area'] = $tmparrinfo[$value['id']];
			$sql = "SELECT opentime FROM tt_areasample WHERE id = ".$value['id'];
			$one = $this->db->one($sql);
			$rowdata['begintime'] = date("Y-m-d H-i-s", $one['opentime']);
			if(time() - strtotime($value['updatetime']) > 30)
			{
				if(empty($busrows[$rowdata['area']][$value['updatetime']]))
				{
					$rowdata['downtime'] = $value['updatetime'];
					$rowdata['durationtime'] = "service outages";
					$rowdata['addtime'] = date("Y-m-d H-i-s", time());
					$result = $this->db->insert ( 'tt_businessserver', $rowdata);
				}
			}
		}
	}
	
	function index() {
		$this->view->display ( "businessdata/businessrecord/index" );
	}
	
	function flexigrid() {
		list ( $colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page ) = $this->common->get_flexigrid_params ();
		$sql = "SELECT COUNT(id) AS num FROM tt_businessserver WHERE {$where}";
		$one = $this->db->one ( $sql );
		$total = $one ['num'];
		$orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
		$sql = "SELECT {$colsinfo} FROM tt_businessserver WHERE {$where}  {$orderby} LIMIT {$offset},{$rp}";
		$this->db->query ( $sql );
		while ( $n = $this->db->get_array () ) {
			$n ['type'] = $this->type [$n ['type']];
			$row [] = array (
					'id' => $n ['id'],
					'cell' => $n 
			);
		}
		$result = array (
				'page' => $page,
				'total' => $total,
				'rows' => $row 
		);
		echo json_encode ( $result );
	}
	
	function add() {
		if (is_submit ()) {
			$rowdata = $_POST['row'];
			
			$rowdata['manager'] = $this->auth->fields['username'];
			$rowdata['addtime'] = date('Y-m-d H:i:s', time());
			$rowdata['lastmodifytime'] = date('Y-m-d H:i:s', time());
			$result = $this->db->insert ( 'tt_businessserver', $rowdata);
			if ($result) {
				insert_log ( translate ( 'MENU_BUSINESSDATA_RECORD' ), translate ( 'ADD' ), $rowdata );
				//success ( translate ( 'C_TIPS_SUCCESS_TEXT' ) );
				cp_tips ("success");
			} else {
				$result = translate ( 'C_TIPS_ERROR_TEXT' );
				error ( $result );
			}
		}
		$this->view->display ( "businessdata/businessrecord/add" );
	}
	function edit($id = 0) {
		$sql = "select * from tt_businessserver where id=$id";
		$row = $this->db->one($sql);
		//var_dump($row);
		//exit(0);

		if (is_submit()) {
			$rowdata = $_POST['row'];
			
			$rowdata['manager'] = $this->auth->fields['username'];
			$rowdata['lastmodifytime'] = date('Y-m-d H-i-s', time());
			
			$result = $this->db->update('tt_businessserver', $rowdata, array('id' => $id));
			if ($result) {
				insert_log(translate('MENU_BUSINESSDATA_RECORD'),translate('EDIT'), $one);
				cp_tips ("success");
			} else {
				$result = translate('C_TIPS_ERROR_TEXT');
				error($result);
			}
		}
		$this->view->display("businessdata/businessrecord/edit", array('row' => $row));
	}
	function del($id = 0) {
		$c = 0;
		$r = '';
		$where = array('id' => $id);
		if ($this->db->delete('tt_businessserver', $where)) {
			insert_log(translate('MENU_BUSINESSDATA_RECORD'),translate('DEL'), array('id'=>$id));
			$c = 1;
			$r = translate('C_TIPS_SUCCESS_TEXT');
		
		} else {
			$r = translate('C_TIPS_ERROR_TEXT');
		}
		json_output($c, $r);
	}
	
	function multi() {
		$type = $this->input->post("type");
		$ids = $this->input->post("ids");
		$c = 0;
		$r = '';
		if (count(explode(',', $ids)) == 0) {
			$r = translate('C_INVALID_PARAMS');
		} else {
			if ($type == 'delete') {
				$this->db->query("DELETE FROM tt_businessserver WHERE id IN ({$ids})");
				insert_log(translate('MENU_BUSINESSDATA_RECORD'),translate('MULTI_DEL'),array('ids'=> $ids));
				$c = 1;
			} else {
				$r = translate('C_INVALID_PARAMS');
			}
		}
		json_output($c, $r);
	}
}







?>